package svn;

import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.*;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.io.File;
import java.util.Collection;

public class SVNLogToClipboard {
    public static void main(String[] args) {
        // 初始化SVN库
        setupLibrary();

        // SVN服务器URL
        String svnUrl = "https://192.168.0.71/svn/S深圳市卡司通展览股份"; // 替换为您的SVN服务器URL

        // 设置身份验证信息，如果需要的话
        ISVNAuthenticationManager authManager = SVNUtil.getAuthManager();

        try {
            SVNURL svnRepositoryURL = SVNURL.parseURIDecoded(svnUrl);

            // 创建SVNRepository实例
            SVNRepository repository = SVNRepositoryFactory.create(svnRepositoryURL);
            repository.setAuthenticationManager(authManager);

            // 获取仓库的日志信息
            long startRevision = 0;
            long endRevision = -1; // 最新版本
            Collection<SVNLogEntry> logEntries = repository.log(new String[]{""}, null, startRevision, endRevision, true, true);

            // 构建需要复制的信息
            StringBuilder clipboardData = new StringBuilder();
            for (SVNLogEntry logEntry : logEntries) {
                clipboardData.append("Revision: ").append(logEntry.getRevision()).append("\n");
                clipboardData.append("Author: ").append(logEntry.getAuthor()).append("\n");
                clipboardData.append("Date: ").append(logEntry.getDate()).append("\n");
                clipboardData.append("Log Message: ").append(logEntry.getMessage()).append("\n\n");
            }

            // 将信息复制到剪贴板
            StringSelection selection = new StringSelection(clipboardData.toString());
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(selection, null);

            System.out.println(clipboardData.toString());
        } catch (SVNException e) {
            e.printStackTrace();
        }
    }

    private static void setupLibrary() {
        // 初始化SVN库
        DAVRepositoryFactory.setup();
        SVNRepositoryFactoryImpl.setup();
        FSRepositoryFactory.setup();
    }
}
